/**
 * Created by chuck7 on 16/9/21.
 */
"use strict"
const utils = require('../utils/index'),
  mw = require('../middlewares/index.js');
const Other = require('../models/other.js');
const Trd = require('../models/trd.js');
module.exports.init = function* (router) {
  utils.print("other-init");
  yield seed;
  router.get('/other/:time',getOther);
  router.get('/change',ChangeNumber);
  // router.patch('/other', mw.verify_token, modify);
}
//生成"关于我"页面的原始数据
function* seed(){
  let other = yield Other.find().exec().catch(err => {
    utils.logger.error(err);
    throw(new Error('数据seed失败,请debug后重新启动'));
  });
}
function* ChangeNumber(){
  let trd = yield Trd.find().exec().catch(err => {
    utils.logger.error(err);
    throw(new Error('数据seed失败,请debug后重新启动'));
  });
  trd.forEach(function(x) {
    console.log(x)
    x.trd_amt_1d = parseInt(x.trd_amt_1d) ;//将String转为Int
    x.save();
  })
}
function* getOther(){
  utils.print("getOther");
  const time = this.params.time;
  let other = yield Other.find({time:time}).exec().catch(err => {
    utils.logger.error(err);
    throw(new Error('数据seed失败,请debug后重新启动'));
  });
  utils.print(other);
  for(let i=0;i<other.length;i++){
    let item = other[i];
    utils.print(item.time);
    for(let j=0;j<item.data.length;j++){
      let dataItem = item.data[j];
      utils.print(dataItem);
      let trd = new Trd({
        "employee_id" : dataItem.employee_id,
        "employee_name" : dataItem.employee_name,
        "merchant_name" : dataItem.merchant_name,
        "merchant_pid" : dataItem.merchant_pid,
        "trd_amt_1d" : dataItem.trd_amt_1d,
        "trd_amt_30d" : dataItem.trd_amt_30d,
        "trd_amt_7d" : dataItem.trd_amt_7d,
        "trd_cnt_1d" : dataItem.trd_cnt_1d,
        "trd_cnt_30d" : dataItem.trd_cnt_30d,
        "trd_cnt_7d" : dataItem.trd_cnt_7d,
        time:item.time
      })
      yield trd.save().catch(err => {
        utils.logger.error(err);
        throw(new Error('数据seed失败,请debug后重新启动'));
      });
    }
  }
}
